<?php

namespace Api\Provider;

use Api\Service\ReturnMsgService;
use Api\Provider\Admin\ConstantProvider;
use Think\Controller;

/**
 * 接口基础类
 */
class IndexProvider extends Controller
{

  /**
   * 调用方法
   * @var type
   */
  protected $method;
  protected $apiArr = [
    //用户模块
    'user' => [
      "provider_name" => "UserProvider",
      "method_list" => [
        "user_info" => ['auth' => false],//'auth'=>true,'noAuthContinue'=>true,'is_paging'=>true
        "save" => ['auth' => false],
        "getUserInfo" => ['auth' => false],
        "getUserRecord" => ['auth' => false],
        "userChargeSet" => ['auth' => false]
      ]
    ],
    //数据迁移
    'data_transfer' => [
      "provider_name" => "DataTransferProvider",
      "method_list" => [
        "transfer" => ['auth' => false],//'auth'=>true,'noAuthContinue'=>true,'is_paging'=>true
      ]
    ],
    //数据清洗
    'data_clean' => [
      "provider_name" => "DataCleanProvider",
      "method_list" => [
        "clean_sheet" => ['auth' => false],//'auth'=>true,'noAuthContinue'=>true,'is_paging'=>true
      ]
    ],
    //谱单模块
    'score' => [
      "provider_name" => "ScoreProvider",
      "method_list" => [
        "index" => ['auth' => false],
        "getScoreByUid" => ['auth' => false],
        "getScoreHot" => ['auth' => false],
        "getScoreFree" => ['auth' => false],
        "getScoreNew" => ['auth' => false],
        "getScoreDetailByScoreId" => ['auth' => false],
        "getFavScoreByUid" => ['auth' => false],
        "addScore" => ['auth' => false],
        "getScoreBasicInfo" => ['auth' => false],
        "editScore" => ['auth' => false],
        "delScore" => ['auth' => false],
        "addFavScore" => ['auth' => false],
        "getScoreBySearchName" => ['auth' => false],
        "addComment" => ['auth' => false],
        "getComment" => ['auth' => false],
        "delComment" => ['auth' => false],
      ]
    ],
    'sheet' => [
      "provider_name" => "SheetProvider",
      "method_list" => [
        "getSheetDetailBySheetId" => ['auth' => false],
        "getSheetBySearchName" => ['auth' => false],
        "addFavSheet" => ['auth' => false],
        "getFavSheet" => ['auth' => false],
        "addComment" => ['auth' => false],
        "getComment" => ['auth' => false],
        "delFromScore" => ['auth' => false],
        "collectSheet" => ['auth' => false],
        "saveRecordUrl" => ['auth' => false],
        'getSheetRecord' => ['auth' => false],
        "delRecord" => ['auth' => false],
        "playCount" => ['auth' => false],
        "dailyRank" => ['auth' => false],
        "addToWeekRank" => ['auth' => false],
        "weeklyRank" => ['auth' => false],
        "personalRankingList" => ['auth' => false],
      ]
    ],
    //分享模块
    'share' => [
      "provider_name" => "ShareProvider",
      "method_list" => [
        "open" => ['auth' => false],
      ]
    ],
    //曲谱图片模块
    'image_sheet' => [
      "provider_name" => "ImageSheetProvider",
      "method_list" => [
        "save" => ['auth' => false],
        "syncOssToServer" => ['auth' => false],
      ]
    ],
    //oss上传
    'oss' => [
      "provider_name" => "OssProvider",
      "method_list" => [
        "get" => ['auth' => false],
      ]
    ],
    //订单模块
    'order' => [
      "provider_name" => "OrderProvider",
      "method_list" => [
        "wxPayment" => ['auth' => false],
        "replyNotify" => ['auth' => false],
        "coinCharge" => ['auth' => false],
        "reduceOneCoin" => ['auth' => false]
      ]
    ],
    //二维码模块
    'qrcode' => [
      "provider_name" => "QRCodeProvider",
      "method_list" => [
        "getQrCode" => ['auth' => false],
      ]
    ]
  ];

  /**
   * 接口日志信息
   * @var type
   */
  public $apiLog = array();

  /**
   * 返回数据的数组,json的数据源
   * @var type
   */
  protected $result = array();


  /**
   * 方法调用、签名校验 控制方法
   * @param type $method
   * @param type $param 内部调用参数
   * @return type
   */
  public function method($action, $method, $param = array())
  {
    //签名校验
    $this->createSign($param);

    //判断是接口否存在
    $ThisClassObj = '';
    if (isset($this->apiArr[$action]['method_list'][$method])) {
      $className = __NAMESPACE__ . '\\Admin\\' . $this->apiArr[$action]['provider_name'];
      $ThisClassObj = new $className();
    }

    if (empty($ThisClassObj)) {
      $this->fail(1);
    }
    // 参数验证
    $this->verifyPublicParams($param);

    // 登录验证
    $noAuthContinue = false;
    isset($this->apiArr[$action]['method_list'][$method]['noAuthContinue']) && $noAuthContinue = $this->apiArr[$action]['method_list'][$method]['noAuthContinue'];
    $isLogin_member = $this->apiArr[$action]['method_list'][$method]['auth'];
    if ($isLogin_member) {
      $param = $this->checkAuthToken($param, $noAuthContinue);
    }
    //处理是否分页
    $is_paging = false;
    isset($this->apiArr[$action]['method_list'][$method]['is_paging']) && $is_paging = $this->apiArr[$action]['method_list'][$method]['is_paging'];
    //公共参数
    $param = $this->commonParams($param, $is_paging);

    $ThisClassObj->$method($param);
  }

  /**
   * 签名验证
   * @param param
   * @return false or true
   */
  protected function createSign($param)
  {
    return true;
  }

  /**
   * 公共参数验证
   * @param param
   * @return false or true
   */
  protected function verifyPublicParams($param)
  {
    return true;
  }

  /**
   * 统一返回Json格式数据
   * @return type
   */
  protected function back()
  {
    $this->apiLog['end_time_unix'] = time(); //结束时间
    $this->apiLog['used_time'] = (($this->apiLog['end_time_unix'] * 10000) - ($this->apiLog['start_time_unix'] * 10000)) / 10000; //总耗时 单位：秒
    $this->apiLog['return_param'] = json_encode($this->result); //返回参数
    $this->ajaxReturn($this->result);
    die;
  }

  /**
   * 校验token有效性
   * @param $param
   * @return boolen
   */
  protected function checkAuthToken($param, $noAuthContinue)
  {
    return $param;
  }

  /**
   * 获取公共参数
   * @param  $param
   */
  protected function commonParams($param, $is_paging)
  {
    //处理分页
    if ($is_paging) {
      if (isset($param['page']) && intval($param['page']) > 0) {
        $page = empty($param['page']) ? 1 : intval($param['page']);
        $rows_size = empty($param['size']) ? 10 : intval($param['size']);;
      } else {
        $page = 1;
        $rows_size = 10;//默认值
      }
      //读取行数
      $param['size'] = $rows_size;
      //当前页
      $param['page'] = $page;
    }
    //用户经纬度
    if (!isset($param['lat']) || empty($param['lat']))
      $param['lat'] = 0;//纬度
    if (!isset($param['lng']) || empty($param['lng']))
      $param['lng'] = 0;//经度
    return $param;
  }

  /**
   * 失败返回
   * @return type
   */
  protected function fail($msgID, $data = array(), $msgDatas = array())
  {
    $this->result = ReturnMsgService::getMsg($msgID);
    $this->result['data'] = $data;
    if (!empty($msgDatas)) {
      foreach ($msgDatas as $index => $data) {
        $this->result['msg'] = $data;
        //$this->result['msg'] = str_replace('{'.$index.'}', $data, $this->result['msg']);
      }
    }
    $this->back();
  }

  /**
   * 成功返回
   * @return type
   */
  protected function success($data, $msgID = 200, $msgDatas = array())
  {
    $this->result = ReturnMsgService::getMsg($msgID);
    $this->result['data'] = $data;
//         	if (!empty($msgDatas)) {
//             	foreach ($msgDatas as $index=>$data) {
//             	    $this->result['msg'] = $this->result['msg'];
//             		//$this->result['msg'] = str_replace('{'.$index.'}', $data, $this->result['msg']);
//             	}
//         }
    $this->back();
  }
}
